﻿using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Utility
{
    public static class EmailHelper
    {
        /// <summary>
        /// 发送邮件 无附件
        /// </summary>
        /// <param name="To"></param>
        /// <param name="Title"></param>
        /// <param name="Body"></param>
        /// <returns></returns>
        public static async Task SendAsync(string To, string Title, string Body,string SystemName,string Email,string EmailSmtpPassWord)
        {
            var message = new MimeMessage();

            message.From.Add(new MailboxAddress(SystemName, Email));
            message.To.Add(new MailboxAddress(To, To));
            message.Subject = Title;

            var builder = new BodyBuilder();
            builder.HtmlBody = Body;  //html
            builder.TextBody = Title;  //文本
            message.Body = builder.ToMessageBody();
            var client = new SmtpClient();

            await client.ConnectAsync("smtp.qq.com", 0, true);     //连接服务
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            await client.AuthenticateAsync(Email, EmailSmtpPassWord); //验证账号密码
            await client.SendAsync(message);
            await client.DisconnectAsync(true);
        }


    }
}
